al();

function al(){
	$.ajax({
		type:"post",
		url:"all",
		data:{},
		success:function(data){
			
			var tableStr = "";
			 
			 for (var i = 0; i < data.length; i++) {
				tableStr += "<tr><td><input type='checkbox' value='"+data[i].t_id+"' /></td>";
				tableStr += "<td>"+data[i].t_id+"</td>";
				tableStr += "<td>"+data[i].t_tiele+"</td>";
				tableStr += "<td>"+data[i].t_time+"</td>";
				tableStr += "<td>"+data[i].uname+"</td>";
				tableStr += "<td>"+data[i].p_name+"</td>";
				tableStr += "<td>"+data[i].t_priority+"</td>";
				tableStr += "<td><button type='button' class='btn btn-outline-dark' data-toggle='modal' data-target='#Modal' onClick='up(this)'>修改</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type='button' class='btn btn-outline-danger' onClick='onDel("+data[i].t_id+")' data-toggle='modal' data-target='#myModal'>删除</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type='button' class='btn btn-info' data-toggle='modal' data-target='#dal' onClick='dan("+data[i].t_id+")'>查看</button></td>";
			
			}
			 var tht="<tr><th style='width:20px;'><label class='i-checks m-b-none'><input type='checkbox'><i></i></label></th><th>编号</th><th>任务标题</th><th>创建时间</th><th>发送者</th><th>执行人</th><th>优先级</th><th>操作</th></tr>";
			$("#tab").empty().append(tht).append(tableStr); 
	 
			
		},
		    error:function(error){
			console.log(error);
			
		}

	})
	
	$(".username").text(localStorage.getItem("a"));
}
//删除
function onDel(id){
     $(".s").text(id);
     
} 
function del(){
	var id=$(".s").text();
	var task={
			tId:id
	}
/* 	$.ajax({
		type : "post",
		url : "del",
		data:JSON.stringify(task),
		contentType:"application/json;charset=utf-8",
		success : function(data) {
			if(data.status=="success"){
				console.log(0);
				alert("删除成功");
				//刷新表格
				al();
			}else{
				alert("删除失败");
			}
		},
		error : function(error) {
			console.log(1);
		}
	})  */
	$.post("del",task,function(data){
			console.log(data);
		if(data.status=="success"){
			alert("删除成功");
			//刷新表格
			al();
		}else{
			alert("删除失败");
		}
	});
}
function up(thi){
	var tId=thi.parentNode.parentNode.children[1].innerText;
	var ttite=thi.parentNode.parentNode.children[2].innerText;
	
	
	$("#ie").text(tId);
	$("#biao").val(ttite)
	
      $.ajax({
    	  type:"post",
    	  url:"pall",
    	  data:{},
    	  success:function(data){
    		  var s=""
    		  console.log(data);
    		  for (var i = 0; i < data.length; i++) {
    			  s+="<option value='"+data[i].pId+"'>"+data[i].pName+"</option>"
    		  }
    		  $("#as").empty().append(s); 
    	  }
      })
      
      
}
//修改
function update(){
	var t=$("#ie").text();
	var ti=$("#biao").val();
	var op=$("#as").val(); 
	var o=$("#aq").find("option:selected").text();
	var task={
			tId:t,
			tTiele:ti,
			tMan:op,
			tPriority:o
	}
	console.log(task);
	 $.ajax({
		type:"post",
  	    url:"updat",
  	    data:JSON.stringify(task),
		contentType:"application/json;charset=utf-8",
  	  success:function(data){
  		if(data.status=="success"){
			console.log(0);
			alert("修改成功");
			//刷新表格
			al();
		}else{
			alert("修改失败");
		}
  	  }
	}) 

}
//查看
function dan(tId){
	
	$.ajax({
		type:"post",
		url:"dan",
		data:{tId},
		success:function(data){
			console.log(data);
			
			
			$("#title").empty().append(data[0].t_tiele);
		    $("#ktime").empty().append(data[0].t_time);
			$("#man").empty().append(data[0].p_name);
			$("#shuo").empty().append(data[0].t_explain);
			$("#gong").empty().append(data[0].t_relationship);
            $("#endtime").empty().append(data[0].t_endtime);
            $("#you").empty().append(data[0].t_priority);
            
		}
	})
	
}

function cha(){
	var pName=$(".form-control").val();
	console.log(pName)
	$.ajax({
		type:"post",
		url:"pName",
		data:{pName},
		success:function(data){
			console.log(data);
			if(data==null){
				
				alert("没有找到");
				//刷新表格
				
			}else{
				var tableStr = "";
				 
				 for (var i = 0; i < data.length; i++) {
					tableStr += "<tr><td><input type='checkbox' value='"+data[i].t_id+"' /></td>";
					tableStr += "<td>"+data[i].t_id+"</td>";
					tableStr += "<td>"+data[i].t_tiele+"</td>";
					tableStr += "<td>"+data[i].t_time+"</td>";
					tableStr += "<td>"+data[i].uname+"</td>";
					tableStr += "<td>"+data[i].p_name+"</td>";
					tableStr += "<td>"+data[i].t_priority+"</td>";
					tableStr += "<td><button type='button' class='btn btn-outline-dark' data-toggle='modal' data-target='#Modal' onClick='up(this)'>修改</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type='button' class='btn btn-outline-danger' onClick='onDel("+data[i].t_id+")' data-toggle='modal' data-target='#myModal'>删除</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type='button' class='btn btn-info' data-toggle='modal' data-target='#dal' onClick='dan("+data[i].t_id+")'>查看</button></td>";
				
				}
				 var tht="<tr><th style='width:20px;'><label class='i-checks m-b-none'><input type='checkbox'><i></i></label></th><th>编号</th><th>任务标题</th><th>创建时间</th><th>发送者</th><th>执行人</th><th>优先级</th><th>操作</th></tr>";
				$("#tab").empty().append(tht).append(tableStr); 
			}
		}
	})
	
}